package com.friehome.app;

import java.io.FileInputStream;
import java.nio.channels.GatheringByteChannel;
import java.util.ArrayList;
import java.util.List;

import model.LTModel;

import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.common.Callback.CancelledException;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import com.dialog.IPDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.HttpRequestUtil;
import com.net.HttpResponse;
import com.net.TTHttpRequestControll;
import com.utils.Comm;
import com.utils.Const;
import com.utils.DownLoadTool;
import com.utils.PreferenceUtils;
import com.utils.TTUtils;

import adapter.IndexAdapter;
import android.content.Intent;
import android.gesture.Prediction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class IndexActivity extends BaseActivity{

	@ViewInject(R.id.text_title)
	TextView text_title;
	
	@ViewInject(R.id.image)
	ImageView image;
	
	@ViewInject(R.id.girdView)
	GridView gridView;
	
	List<LTModel> data = new ArrayList<LTModel>();
	
	IndexAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_index);
		x.view().inject(this);
		
		WindowManager wm = this.getWindowManager();
		Comm.pWidth = wm.getDefaultDisplay().getWidth();
		Comm.pHeight = wm.getDefaultDisplay().getHeight();
		
		
		
		adapter = new IndexAdapter(getApplicationContext(), data);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int p,
					long arg3) {
				try {
					Intent intent = new Intent(IndexActivity.this, Main2Activity.class);
					intent.putExtra("id", data.get(p).id);
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		
		getData();
		
		Log.e("w", ""+ image.getWidth());
		Log.e("h",""+ image.getHeight());
		
		ImageOptions op = new ImageOptions.Builder()
		 .setSize(630, 405)
	    .setRadius(0)
	    // 如果ImageView的大小不是定义为wrap_content, 不要crop.
	    .setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
	    // 加载中或错误图片的ScaleType
	    //.setPlaceholderScaleType(ImageView.ScaleType.FIT_CENTER)
	    .setImageScaleType(ImageView.ScaleType.FIT_XY)
	    
	    .build();
		 x.image().bind(image, PreferenceUtils.getString(getApplicationContext(), Const.SERVICE, Const.SERVICE_URL)+"image/bg.png",op);

		
//		new DownLoadTool(new DownLoadTool.DownLoadCallBack() {
//			
//			@Override
//			public void onBack(boolean result, String path) {
//				try {
//					if (result) {
//						 FileInputStream inStream = new FileInputStream(path);
//						 BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
//					     decodeOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
//					     decodeOptions.inDensity = 0;
//					
//					     Bitmap bitmap = BitmapFactory.decodeStream(inStream,null, decodeOptions);
//						 image.setImageDrawable(new android.graphics.drawable.BitmapDrawable(getResources(), bitmap));
//						
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//				
//				
//			}
//		}).execute(PreferenceUtils.getString(getApplicationContext(), Const.SERVICE, Const.SERVICE_URL)+"image/bg.png");
	}
	
	public void getData(){
		
		toast("当前地址:" + PreferenceUtils.getString(getApplicationContext(), Const.SERVICE, Const.SERVICE_URL));
		TTHttpRequestControll.httpIndex(getApplicationContext(), new HttpRequestUtil.HttpRonspontCallBack() {
			
			@Override
			public void callBack(HttpResponse result) {
				
				try {
					if (result.success) {
						JSONObject ob=  new JSONObject(result.content);
						data = new Gson().fromJson(ob.getJSONArray("list").toString(), new  TypeToken<List<LTModel>>(){}.getType() );

						DbManager db = TTUtils.getDbManager();
						db.delete(LTModel.class);
							for (LTModel l : data) {
							
								db.saveOrUpdate(l);
							}
						
							Log.e("data", "" + new Gson().toJson(data));
						
						adapter = new IndexAdapter(getApplicationContext(), data);
						gridView.setAdapter(adapter);
						if (data.size() == 0) {
							toast("无礼厅数据");
						}
					}else{
						toast(result.errorMsg);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
					
				
			}
		});
		
	}
	
	@Event(value={R.id.btn_setting})
	private void onClick(View v) {
		
		IPDialog dialog = new IPDialog(this, "", new IPDialog.DialogClickListener() {
			
			@Override
			public void onBack(String ip, String port) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(ip) || TextUtils.isEmpty(port)) {
					toast("地址不为空!!");
				}else{
					PreferenceUtils.putString(getApplicationContext(), Const.SERVICE, "http://" + ip + ":" + port +"/");
					getData();
				}
			}
		});
		dialog.showDialog();
	}
	 
}
